/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.zjdt.epserver.db.service4mysql;

import com.zjdt.epserver.db.dao.CargoDao;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;

/**
 *
 * @author dusin
 */
public class CargoServiceImpl {
    static Logger logger = Logger.getLogger(CargoServiceImpl.class);
    CargoDao cargoDao;

    public CargoServiceImpl(CargoDao cargoDao) {
        this.cargoDao = cargoDao;
    }
     
    public List<Map<String,Object>> findAll(int start,int size){
        return cargoDao.findAll(start,size);
    }   
    
    public boolean create(String recordNo,String plateNo,int siterID,String factoryCode,
            String arrivalTime,String rid){
        return cargoDao.doCreate(recordNo, plateNo, siterID, factoryCode, arrivalTime,rid);
    }
    
    public boolean create(String  recordNo,String plateNo,int siterID,String factoryCode,
            String arrivalTime,String grossWeight,String tareWeight,String netWeight,String minusImpurity,
            String companyCode,String productCode){
        return cargoDao.doCreate(recordNo,plateNo,siterID,factoryCode,arrivalTime,grossWeight,tareWeight,netWeight,minusImpurity,companyCode,productCode);
    }   
    public boolean updateData(int ID,String  recordNo,String plateNo,int siterID,String factoryCode,
            String arrivalTime,String grossWeight,String tareWeight,String netWeight,String minusImpurity,
            String companyCode,String productCode){
        return cargoDao.doUpdateData(ID,recordNo,plateNo,siterID,factoryCode,arrivalTime,grossWeight,tareWeight,netWeight,minusImpurity,companyCode,productCode);
    }   
    public boolean updatePhotoNameByRecordNo(String recordNo,String type,String filename){
        return cargoDao.doUpdatePhotoNameByRecordNo(recordNo, type, filename);
    }
    
    public boolean UpdatePaperDataByRecordNo(String recordNo,double grossWeight,double tareWeight,double minusImpurity,
            double minusWater,double netWeight,int unit,String productCode,String companyCode){
        return cargoDao.doUpdatePaperDataByRecordNo(recordNo, grossWeight, tareWeight, 
                minusImpurity, minusWater, netWeight, unit,productCode,companyCode);
    }

    public boolean updateStatusBitsById(int id,int status){
        return cargoDao.doUpdateBitById(id, status);
    }
    
    public boolean updateStatusBitsByRecordNo(String recordNo,int status){
        return cargoDao.doUpdateBitByRecNo(recordNo, status);
    }
    //根据多种条件拼凑where从句，并填写参数（？替换的值）
    private String buildWhere(Integer ID,String companyCode,String factoryCode, String dateFrom, String dateTo, String key,List<Object> argList){
        StringBuilder sb = new StringBuilder();
        //List<Object> argList = new ArrayList<Object>(); //组织参数值
        if(ID!=null){
           sb.append(" and ID>?");
           argList.add(ID);
        }
        if(companyCode!=null&&!"".equals(companyCode)&&!"请选择供应商".equals(companyCode)){
            sb.append(" and companyCode=?");
            argList.add(companyCode);
        }
        if(factoryCode!=null&&!"".equals(factoryCode)&&!"请选择客户".equals(factoryCode)){
            sb.append(" and factoryCode=?");
            argList.add(factoryCode);
        }
        //有开始日期，没有结束日期，则查询当前开始日期的记录
        if((dateFrom!=null&&!dateFrom.trim().equals(""))&&(dateTo==null||dateTo.trim().equals(""))){
            if(dateFrom.length()<12){
                //dateFrom = dateFrom.replaceAll("-", "");            
                sb.append(" and ");            
                sb.append(" substr(arrivalTime,1,10)=?");
                argList.add(dateFrom);
            }else{  //目前这里给大屏查询用
                //dateFrom = dateFrom.replaceAll("[-: ]", "");            
                sb.append(" and ");            
                sb.append(" arrivalTime>?");
                argList.add(dateFrom);
            }
        }else if((dateFrom!=null&&!dateFrom.trim().equals(""))&&(dateTo!=null&&!dateTo.trim().equals(""))){
            //开始日期和结束日期都有
            //dateFrom = dateFrom.replaceAll("-", "");
            dateTo = dateTo+" 24:00:00";            
            sb.append(" and ");         
            sb.append(" arrivalTime>? and arrivalTime<?");
            argList.add(dateFrom);
            argList.add(dateTo);
        }else if((dateTo!=null&&!dateTo.trim().equals(""))&&(dateFrom==null||dateFrom.trim().equals(""))){
            //只有结束日期，则查询结束日期当天的数据
            //dateTo = dateTo.replaceAll("-", "");            
            sb.append(" and ");            
            sb.append(" substr(arrivalTime,1,10)=?");
            argList.add(dateTo);
        }
        //模糊查询
        if(key!=null && !key.equals("")){            
            sb.append(" and ");        
            sb.append(" (recordNo like ? or plateNo like ? or siterName like ? or factoryName like ? or productName like ? or companyName like ?) ");
            argList.add("%"+key+"%");
            argList.add("%"+key+"%");
            argList.add("%"+key+"%");
            argList.add("%"+key+"%");
            argList.add("%"+key+"%");
            argList.add("%"+key+"%");            
        }
        return sb.toString();
    }
    //查找记录数
    public int findCount(Integer ID,String companyCode,String factoryCode, String dateFrom, String dateTo, String key){
        List<Object> argList = new ArrayList<Object>(); //组织参数值
        String sql = " select count(*) from vcargo where 1=1 "+buildWhere(ID,companyCode,factoryCode, 
                dateFrom, dateTo, key,argList);
        return cargoDao.findCount(sql, argList.toArray());        
    }
    
    public List<Map<String, Object>> findAll(Integer ID,String companyCode,String factoryCode, String dateFrom, String dateTo, String key, int i, int rows) {
        StringBuilder sb = new StringBuilder();   //组织sql语句
        sb.append("select * from vcargo ");
        sb.append("where 1=1").append(" ");
        List<Object> argList = new ArrayList<Object>(); //组织参数值
        sb.append(buildWhere(ID,companyCode,factoryCode, 
                dateFrom, dateTo, key,argList));       
        sb.append(" order by arrivalTime desc ");
        sb.append( " limit ").append(i).append(",").append(rows);
        return cargoDao.findAll(sb.toString(), argList.toArray());
    }
    
    public List<Map<String, Object>> findAll4BigScreen(Integer ID,String companyCode,String factoryCode, String dateFrom, String dateTo, String key, int i, int rows) {
        StringBuilder sb = new StringBuilder();   //组织sql语句
        sb.append("select * from vcargo ");
        sb.append("where 1=1 and status&65536!=0").append(" ");
        List<Object> argList = new ArrayList<Object>(); //组织参数值
        sb.append(buildWhere(ID,companyCode,factoryCode, 
                dateFrom, dateTo, key,argList));       
        sb.append(" order by ID desc ");
        sb.append( " limit ").append(i).append(",").append(rows);
        return cargoDao.findAll(sb.toString(), argList.toArray());
    }
    
    public boolean remove(int id){
        return cargoDao.doDeleteById(id);
    }    
    
    public List<Map<String,Object>> findPathByRid(String rid){
        return cargoDao.findPathByRID(rid);
    }
    
    public List<Map<String,Object>> findAllRunningTrucks(String factoryCode){
        return cargoDao.findAllRunningTrucks(factoryCode);
    }
}
